package com.sx.micowell.framework.view.dialog

import android.content.Context
import com.example.mvvm.rx.RxViewUntil
import com.sx.micowell.R
import com.sx.micowell.databinding.DialogPowerSettingBinding
import com.sx.micowell.framework.model.Sport2Model
import com.sx.micowell.framework.tools.CacheUtils
import com.sx.widget.app.dialog.BaseDialog
import java.math.RoundingMode

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/12/4 9:38
 */
class DialogPowerSetting {
    class Builder(context: Context, viewModel: Sport2Model) :
        BaseDialog.Builder<DialogPowerSetting.Builder>(context) {
        var bind: DialogPowerSettingBinding? = null
        var currentValue = 0
        var viewModel: Sport2Model? = null
        override fun show(): BaseDialog {
            currentValue = viewModel?.powerLiveData?.value as Int
            bind?.progress?.setProgress(
                currentValue.toBigDecimal()
                    .divide(45.toBigDecimal(), 2, RoundingMode.CEILING)
                    .multiply(100.toBigDecimal()).toInt(), ""
            )
            bind?.value?.text = currentValue.toString()
            return super.show()

        }

        init {
            this.viewModel=viewModel
            setContentView(R.layout.dialog_power_setting)
            bind = DialogPowerSettingBinding.bind(contentView)
            setCanceledOnTouchOutside(false)
            //最大值45
            bind?.tvAdd?.setOnClickListener {
                if (currentValue >= 45) {

                } else {
                    currentValue = currentValue + 1
                    bind?.progress?.setProgress(
                        currentValue.toBigDecimal()
                            .divide(45.toBigDecimal(), 2, RoundingMode.CEILING)
                            .multiply(100.toBigDecimal()).toInt(), ""
                    )
                    bind?.value?.text = currentValue.toString()
                }
            }
            bind?.tvSub?.setOnClickListener {
                if (currentValue <= 1) {

                } else {
                    currentValue = currentValue - 1
                    bind?.progress?.setProgress(
                        currentValue.toBigDecimal()
                            .divide(45.toBigDecimal(), 2, RoundingMode.CEILING)
                            .multiply(100.toBigDecimal()).toInt(), ""
                    )
                    bind?.value?.text = currentValue.toString()
                }
            }
            RxViewUntil.setClickShake(bind?.start) {
                viewModel.powerLiveData.value = currentValue
                dismiss()
            }
        }
    }
}